package gossipinator;

// Ce package contient les elements de base
import javax.microedition.midlet.*;

// Ce package contient les elements permettant de gerer l'interface graphique
import javax.microedition.lcdui.*;

import gossipinator.bt.*;

// Pour etre executable, la classe doit heriter de MIDlet
public class Controller extends MIDlet implements CommandListener, ItemCommandListener {
    private Display display;
    private ScreenStart screenStart;
    private GossipScreen gossipScreen;
    public static String gossiplist1 = new String("\r\n");
    public static BT bt1;
    public static Thread sThread,cThread;
    public static boolean clientdidntfindserver;


    // Constructeur
    // Construit l'interface graphique
    public Controller() {

        //grizou is cool
        this.screenStart = new ScreenStart(this);
        this.gossipScreen = new GossipScreen(this);

	// fait un lien avec l'affichage
	this.display = Display.getDisplay(this);


    }

  // Evenement execute au demarrage de l'application
  public synchronized void startApp() {
    Gossip gossip1;


      // affichage du formulaire
      display.setCurrent(screenStart.getScreen());

    //Bluetooth init
    bt1 = new BT();

    clientdidntfindserver = true;

    cThread = new ClientThread();
    cThread.start();
    try{
        Thread.sleep(8000);
    } catch (Exception e) {
	System.out.println("thread sleep exception " + e) ;
    }

    if (clientdidntfindserver == true) {
        sThread = new ServerThread();
        sThread.start();
        //cThread.interrupt();
    }



      if(Gossip.initGossip()){

          gossip1 = Gossip.getGossip();
          while (gossip1 != null) {
           //System.out.println(gossip1.getHeader()+gossip1.getMessage());
            gossiplist1 += (gossip1.getHeader()+": "+gossip1.getMessage()+"\r\n");
            gossip1 = Gossip.getGossip();
          }
      }

  }

  // Evenement execute lors de la mise en pause de l'application
  public void pauseApp() {
  }

  // Evenement execute lorsque l'application se termine
  public void destroyApp(boolean unconditional) {
         Gossip.killHash();
         sThread.interrupt();
         cThread.interrupt();
         bt1.Client_Fermer();
         bt1.Serveur_Fermer();
         bt1=null;
  }

  public void commandAction(Command c, Displayable s) {
      // lors du clic sur le bouton Exit
      if (c == screenStart.commandExit)
          {
	  
          // appel manuel a  la fonction de fermeture
	  destroyApp(true);
          // on demande au manager de fermer l'application
	  notifyDestroyed();
	  }
  }

  public void commandAction(Command c, Item i) {
        Gossip gossip1;

        if (c == screenStart.commandPress1) {
             screenStart.label1.setString(screenStart.label1.getString()) ;
	}
        if (c == screenStart.addGossip) {
            gossip1 = new Gossip(screenStart.label1.getString(),screenStart.textfield1.getString());

           /* gossip1 = Gossip.getGossip();
            while (gossip1 != null) {
               //System.out.println(gossip1.getHeader()+gossip1.getMessage());
               gossiplist1 += (gossip1.getHeader()+": "true+gossip1.getMessage()+"\r\n");
               gossip1 = Gossip.getGossip();
            }*/

            gossiplist1 += (gossip1.getHeader() +": "+ gossip1.getMessage()+"\r\n");
            gossipScreen.label1.setText(gossiplist1);
            display.setCurrent(gossipScreen.getScreen());
        }
        if (c == gossipScreen.returnStartScreen) {
            display.setCurrent(screenStart.getScreen());
        }
  }
}
